#include "configscreen.h"
#include <QtGui>

ConfigScreen::ConfigScreen(QWidget *parent) :
  QWidget(parent)
{
  QSettings s;
  QVBoxLayout* mainlayout = new QVBoxLayout;
  setLayout(mainlayout);

  QTabWidget* tab = new QTabWidget(this);
  mainlayout->addWidget(tab);
  QPushButton* bt_save = new QPushButton("Save and Quit");
  mainlayout->addWidget(bt_save);
  connect(bt_save, SIGNAL(clicked()), this, SLOT(SaveConfig()));

  QFormLayout *generalLayout = new QFormLayout;
  QGroupBox* generalBox = new QGroupBox;

  generalBox->setLayout(generalLayout);
  general_appengine_enable = new QCheckBox;
  generalLayout->addRow("App Engine Path", general_appengine_enable);

  general_appengine_path = new QLineEdit;
  general_appengine_path->setDisabled(true);
  generalLayout->addRow("App Engine Path", general_appengine_path);
  general_appengine_path->setText(s.value("general/appengine/path").toString());
  connect(general_appengine_enable, SIGNAL(toggled(bool)),
          general_appengine_path, SLOT(setEnabled(bool)));

  general_appengine_enable->setChecked(
        s.value("general/appengine/enable").toBool());

  QFileDialog* general_appengine_dialog = new QFileDialog(this);
  general_appengine_dialog->setFileMode(QFileDialog::Directory);
  connect(general_appengine_enable, SIGNAL(toggled(bool)),
          general_appengine_dialog, SLOT(setVisible(bool)));
  connect(general_appengine_dialog, SIGNAL(fileSelected(QString)),
          this, SLOT(SetAppenginePath(QString)));

  tab->addTab(generalBox, "General");
  connect(this, SIGNAL(closed()), this, SLOT(hide()));
}

void ConfigScreen::SaveConfig() {
  QSettings s;
  s.setValue("general/appengine/enable", general_appengine_enable->isChecked());
  s.setValue("general/appengine/path", general_appengine_path->text());
  emit closed();
}

void ConfigScreen::SetAppenginePath(QString path) {
  general_appengine_path->setText(path);
}
